package com.liyghting.datastructurealgorithm.queue;

/**
 * 基于链表实现队列
 * 
 * @author LiYiGuang
 * @date 2018/11/22 15:33
 */
public class LinkedQueue {
	private Node head;// 队头
	private Node tail;// 队尾
	// 入队

	public void enqueue(String data) {
		Node node = new Node(data);
		// 队空
		if (tail == null) {
			head = tail = node;
		}
		tail.next = node;
		tail = node;
	}

	// 出队
	public String dequeue() {
		// 队空
		if (head == null)
			return null;
		String data = head.data;
		Node next = head.next;
		if (next == null) {
			head = tail = null;
		} else {
			head = next;
		}
		return data;
	}

	public void printAll() {
		if (head == null)
			return;
		Node node = head;
		while (node != null) {
			System.out.print(node.data + " ");
			node = node.next;
		}
		System.out.println();
	}

	private static class Node {
		String data;
		Node next;

		Node(String data) {
			this.data = data;
		}
	}

	public static void main(String[] args) {
		LinkedQueue queue = new LinkedQueue();
		for (int i = 0; i < 10; i++) {
			queue.enqueue(i + "");
		}
		queue.printAll();
		System.out.println(queue.head.data + "------" + queue.tail.data);

		for (int i = 0; i < 5; i++) {
			queue.enqueue((i + 10) + "");
		}
		queue.printAll();
		System.out.println(queue.head.data + "------" + queue.tail.data);

		for (int i = 0; i < 5; i++) {
			queue.dequeue();
		}
		queue.printAll();
		System.out.println(queue.head.data + "------" + queue.tail.data);
	}

}
